import { options } from 'api/config';
import { ERR_TIMEOUT, ERR_CUSTOM } from 'api/config';
import { userStorageName } from 'common/js/config';
import { removeLocalStorage } from 'common/js/dom';

export function ajaxPost(url, data) {
	return axios.post(url, data, {
		...options(),
		validateStatus: function (status) {
		    return status >= 200;
		},
	})
	.then((res) => {
		if(res.status === ERR_TIMEOUT) {
			alert('登录超时，请重新登录！');
			removeLocalStorage(userStorageName);
			window.location.href = location.pathname;
			return
		}
		return Promise.resolve(res.data)
	})
	.catch((err) => {
		return Promise.reject({
			code: ERR_CUSTOM,
			message: '请检查网络是否畅通，或'
		})
	})
}

export function ajaxPostNoToken(url, data) {
	return axios.post(url, data, {})
		.then((res) => {
			return Promise.resolve(res.data)
		})
		.catch((err) => {
			return Promise.reject({
				code: ERR_CUSTOM,
				message: '请检查网络是否畅通，或'
			})
		})
}